package com.asset.dispoal.file.agreementCaseData.service.commom;

import com.alibaba.excel.EasyExcel;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.business.idcardrefresh.domain.db.mapper.IdCardRefreshMapper;
import com.asset.dispoal.file.agreementCaseData.domain.dto.ExportIdCardRefresh;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;

/**
 * @author qr
 * @date 2022/4/6$
 */
@Component
public class ImportNoIdCardRefreshCommand implements BaseCommand<MultipartFile, Result> {


    @Autowired
    BsCaseMapper caseMapper;

    @Autowired
    IdCardRefreshMapper idCardRefreshMapper;


    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result execute(MultipartFile file) {
        List<ExportIdCardRefresh> exportIdCardRefresheList = EasyExcel.read(file.getInputStream()).head(ExportIdCardRefresh.class).sheet().doReadSync();
        for (ExportIdCardRefresh exportIdCardRefresh : exportIdCardRefresheList) {
            if (exportIdCardRefresh.getResult() != null) {
                caseMapper.updateIdCardState(exportIdCardRefresh.getCaseId(), exportIdCardRefresh.getResult(), "1");
                idCardRefreshMapper.updateCaseResult(exportIdCardRefresh.getRefreshId(), exportIdCardRefresh.getCaseId(), exportIdCardRefresh.getResult());
            }
        }
        return Result.ok();
    }
}
